<!--新增会员-->
<template>
  <section class="webview fixHeight">
    <el-scrollbar style="height:100%">
      <!--      经纪人-->
      <el-form v-if="$route.query.type == 1" ref="form" :model="detail" label-width="100px" class="form">
        <template v-if="$route.query.id">
          <el-form-item label="金币数">
            <div>{{ detail.integral || 0 }}</div>
          </el-form-item>
          <el-form-item label="违规次数">
            <div>{{ detail.violationNum || 0 }}</div>
          </el-form-item>
          <el-form-item label="浏览次数">
            <div>{{ detail.browseCount || 0 }}</div>
          </el-form-item>
          <el-form-item label="分享次数">
            <div>{{ detail.shareCount || 0 }}</div>
          </el-form-item>
        </template>
        <el-form-item label="描述">
          <el-input v-model="detail.aboutUs" />
        </el-form-item>
        <el-form-item label="账号">
          <el-input v-model="detail.account" />
        </el-form-item>
        <el-form-item label="经纪人名字">
          <el-input v-model="detail.name" />
        </el-form-item>
        <el-form-item label="电子邮件">
          <el-input v-model="detail.email" />
        </el-form-item>
        <el-form-item label="入职时间">
          <el-date-picker
            v-model="detail.entryTime"
            type="datetime"
            placeholder="选择日期"
			value-format="yyyy-MM-dd HH:mm"
			format='yyyy-MM-dd HH:mm'
          />
        </el-form-item>
        <el-form-item label="主营小区">
          <el-select v-model="detail.mainResidentialId" multiple placeholder="请选择">
            <el-option
              v-for="item in mainResidentialIdOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="detail.phone" />
        </el-form-item>
        <el-form-item label="所属门店">
          <el-select v-model="detail.storeId" placeholder="请选择">
            <el-option
              v-for="item in storeIdList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="服务年限">
          <el-input v-model="detail.workYear" />
        </el-form-item>
        <el-form-item label="头像">
          <div class="picList">
            <div v-for="(item,index) in fileListHead" class="item">
              <img class="pic" :src="item" alt>
              <i class="el-icon-delete delIcon hand" @click="fileListHead.splice(index, 1);detail.headPortrait=''" />
            </div>
          </div>
          <div v-if="fileListHead.length < 1" class="item itemNo">
            <el-upload
              :action="Is.sevenDomain"
              :multiple="false"
              name="file"
              :limit="1"
              :show-file-list="false"
              :before-upload="handleUpload"
              :on-success="(res,file)=>{if(res.code == 200){fileListHead.push(res.data);detail.headPortrait = fileListHead[0]}}"
            >
              <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，只能上传1张，且不超过10M</div>
              <div class="el-upload el-upload--picture-card">
                <i slot="default" class="el-icon-plus" />
              </div>
            </el-upload>
          </div>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submit">提交</el-button>
          <el-button @click="cancel">返回</el-button>
        </el-form-item>
      </el-form>
      <!--      房产公司-->
      <el-form v-if="$route.query.type == 3" ref="form" :model="detail" label-width="100px" class="form">
        <el-form-item label="账号">
          <el-input v-model="detail.account" />
        </el-form-item>
        <el-form-item label="企业简介">
          <el-input v-model="detail.briefIntroduction" />
        </el-form-item>
        <el-form-item label="法人姓名">
          <el-input v-model="detail.legalName" />
        </el-form-item>
        <el-form-item label="主营业务">
          <el-input v-model="detail.mainBusiness" />
        </el-form-item>
        <el-form-item label="企业名称">
          <el-input v-model="detail.name" />
        </el-form-item>
        <el-form-item label="注册地址">
          <el-input v-model="detail.registeredPlace" />
        </el-form-item>
        <el-form-item label="企业logo">
          <div class="picList">
            <div v-for="(item,index) in fileListLogo" class="item">
              <img class="pic" :src="item" alt>
              <i class="el-icon-delete delIcon hand" @click="fileListLogo.splice(index, 1)" />
            </div>
          </div>
          <div v-if="fileListLogo.length < 1" class="item itemNo">
            <el-upload
              :action="Is.sevenDomain"
              :multiple="false"
              name="file"
              :limit="1"
              :show-file-list="false"
              :before-upload="handleUpload"
              :on-success="(res,file)=>{if(res.code == 200){fileListLogo.push(res.data);detail.logo = fileListLogo[0]}}"
            >
              <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，只能上传1张，且不超过10M</div>
              <div class="el-upload el-upload--picture-card">
                <i slot="default" class="el-icon-plus" />
              </div>
            </el-upload>
          </div>
        </el-form-item>
        <el-form-item label="公司风采">
          <div class="picList">
            <div v-for="(item,index) in fileListGsfc" class="item">
              <img class="pic" :src="item" alt>
              <i class="el-icon-delete delIcon hand" @click="fileListGsfc.splice(index, 1)" />
            </div>
          </div>
          <div v-if="fileListGsfc.length < 1" class="item itemNo">
            <el-upload
              :action="Is.sevenDomain"
              :multiple="false"
              name="file"
              :limit="1"
              :show-file-list="false"
              :before-upload="handleUpload"
              :on-success="(res,file)=>{if(res.code == 200){fileListGsfc.push(res.data);}}"
            >
              <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，只能上传1张，且不超过10M</div>
              <div class="el-upload el-upload--picture-card">
                <i slot="default" class="el-icon-plus" />
              </div>
            </el-upload>
          </div>
        </el-form-item>
        <el-form-item label="营业执照">
          <div class="picList">
            <div v-for="(item,index) in fileListYyzz" class="item">
              <img class="pic" :src="item" alt>
              <i class="el-icon-delete delIcon hand" @click="fileListYyzz.splice(index, 1)" />
            </div>
          </div>
          <div v-if="fileListYyzz.length < 1" class="item itemNo">
            <el-upload
              :action="Is.sevenDomain"
              :multiple="false"
              name="file"
              :limit="1"
              :show-file-list="false"
              :before-upload="handleUpload"
              :on-success="(res,file)=>{if(res.code == 200){fileListYyzz.push(res.data);}}"
            >
              <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，只能上传1张，且不超过10M</div>
              <div class="el-upload el-upload--picture-card">
                <i slot="default" class="el-icon-plus" />
              </div>
            </el-upload>
          </div>
        </el-form-item>
        <el-form-item label="公司介绍">
          <el-upload
            accept="video/mp4"
            :action="Is.sevenDomain"
            :limit="1"
            :file-list="fileListIntroduce"
            :show-file-list="true"
            :before-remove="(res)=>{fileListIntroduce=[];return true}"
            :on-success="(res,file)=>{if(res.code == 200){fileListIntroduce=[];fileListIntroduce.push(res.data);}}"
          >
            <span
              slot="tip"
              class="el-upload__tip"
              style="padding-left:15px;"
            >建议上传mp4格式视频</span>
            <div class="avatarWrap">
              <el-button type="success" round class="button">点击上传</el-button>
            </div>
          </el-upload>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submit">提交</el-button>
          <el-button @click="cancel">返回</el-button>
        </el-form-item>
      </el-form>
      <!--      开发商-->
      <el-form v-if="$route.query.type == 2" ref="form" :model="detail" label-width="100px" class="form">
        <el-form-item label="名称">
          <el-input v-model="detail.name" />
        </el-form-item>
        <el-form-item label="企业logo">
          <div class="picList">
            <div v-for="(item,index) in fileListLogo" class="item">
              <img class="pic" :src="item" alt>
              <i class="el-icon-delete delIcon hand" @click="fileListLogo.splice(index, 1);detail.logo = ''" />
            </div>
          </div>
          <div v-if="fileListLogo.length < 1" class="item itemNo">
            <el-upload
              :action="Is.sevenDomain"
              :multiple="false"
              name="file"
              :limit="1"
              :show-file-list="false"
              :before-upload="handleUpload"
              :on-success="(res,file)=>{if(res.code == 200){fileListLogo.push(res.data);detail.logo = fileListLogo[0]}}"
            >
              <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，只能上传1张，且不超过10M</div>
              <div class="el-upload el-upload--picture-card">
                <i slot="default" class="el-icon-plus" />
              </div>
            </el-upload>
          </div>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submit">提交</el-button>
          <el-button @click="cancel">返回</el-button>
        </el-form-item>
      </el-form>
      <!--      会员-->
      <el-form v-if="$route.query.type == 4" ref="form" :model="detail" label-width="100px" class="form">
        <el-form-item label="头像">
          <div class="picList">
            <div v-for="(item,index) in fileListHead" class="item">
              <img class="pic" :src="item" alt>
              <i class="el-icon-delete delIcon hand" @click="fileListHead.splice(index, 1)" />
            </div>
          </div>
          <div v-if="fileListHead.length < 1" class="item itemNo">
            <el-upload
              :action="Is.sevenDomain"
              :multiple="false"
              name="file"
              :limit="1"
              :show-file-list="false"
              :before-upload="handleUpload"
              :on-success="(res,file)=>{if(res.code == 200){fileListHead.push(res.data);detail.headPortrait = fileListHead[0]}}"
            >
              <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，只能上传1张，且不超过10M</div>
              <div class="el-upload el-upload--picture-card">
                <i slot="default" class="el-icon-plus" />
              </div>
            </el-upload>
          </div>
        </el-form-item>
        <el-form-item label="昵称">
          <el-input v-model="detail.nickName" />
        </el-form-item>
        <el-form-item label="账号">
          <el-input v-model="detail.account" />
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="detail.email" />
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="detail.phone" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submit">提交</el-button>
          <el-button @click="cancel">返回</el-button>
        </el-form-item>
      </el-form>
    </el-scrollbar>
  </section>
</template>
<script>
import { residentialList, houareaList, shopList, memberUpdate, memberAdd, memberDetail, developersAdd, developersDetail, developersUpdate, agentAdd, agentDetail, agentUpdate, housePropertytAdd, housePropertyDetail, housePropertyUpdate } from '@/api/api'
export default {
  data() {
    return {
      fileListHead: [],
      fileListVideo: [],
      fileListLogo: [],
      fileListGsfc: [],
      fileListYyzz: [],
      fileListIntroduce: [],
      storeIdList: [],
      mainResidentialIdOptions: [],
      detail: {}
    }
  },
  created() {
    // 门店列表
    shopList({
      curPage: 1,
      pageSize: 9999,
      name: ''
    }).then(({ data }) => {
      this.storeIdList = data.records
    })
    // 小区列表 需要改一下
    residentialList({
      curPage: 1,
      pageSize: 6666,
      name: ''
    }).then(data => {
      this.mainResidentialIdOptions = data.data.records
    })
    if (this.$route.query.id) {
      // 房产公司
      if (this.$route.query.type == 3) {
        housePropertyDetail(this.$route.query.id).then(({ data }) => {
          this.detail = data
          if (this.detail.logo) {
            this.fileListLogo = [this.detail.logo]
          }
          this.detail.enterpriseResources.forEach((item, index) => {
            if (item.type == 0) {
              this.fileListGsfc.push(item.pictureUrl)
            }

            if (item.type == 1) {
              this.fileListYyzz.push(item.pictureUrl)
            }

            if (item.type == 2) {
              this.fileListIntroduce.push(item.pictureUrl)
            }
          })
        })
      }
      // 经纪人
      if (this.$route.query.type == 1) {
        agentDetail(this.$route.query.id).then(({ data }) => {
          this.detail = data
          if (this.detail.headPortrait) {
            this.fileListHead = [this.detail.headPortrait]
          }
        })
      }
      // 开发商
      if (this.$route.query.type == 2) {
        this.detail = JSON.parse(this.$route.query.id)
        this.fileListLogo = [this.detail.logo]
      }
      // 会员
      if (this.$route.query.type == 4) {
        memberDetail(this.$route.query.id).then(({ data }) => {
          this.detail = data
          if (this.detail.headPortrait) {
            this.fileListHead = [this.detail.headPortrait]
          }
        })
      }
    } else {
      if (this.$route.query.type == 1) {
        this.detail = {
          'aboutUs': '',
          'account': '',
          'email': '',
          'entryTime': '',
          'headPortrait': '',
          'id': '',
          'introduceUrl': '',
          'mainResidentialId': [
          ],
          'name': '',
          'phone': '',
          'storeId': '',
          'workYear': ''
        }
      }
      if (this.$route.query.type == 2) {
        this.detail = {
          'enterpriseType': '',
          'logo': '',
          'name': ''
        }
      }
      if (this.$route.query.type == 3) {
        this.detail = {
          'account': '',
          'briefIntroduction': '',
          'enterpriseResources': [
          ],
          'legalName': '',
          'logo': '',
          'mainBusiness': '',
          'name': '',
          'registeredPlace': ''
        }
      }
      if (this.$route.query.type == 4) {
        this.detail = {
          'account': '',
          'email': '',
          'headPortrait': '',
          'nickName': '',
          'phone': ''
        }
      }
    }
  },
  methods: {
    handleRemove(index) {
      this.fileList.splice(index, 1)
    },
    // 上传图片之前的验证
    handleUpload() {
    },
    // 上传成功之后的回调
    handleSuccess(res) {
      if (res.code === 200) {
        this.fileList.push(res.data)
        this.detail.imgLink = this.fileList[0]
      }
    },
    // 添加/编辑
    submit() {
      // 验证不通过则不提交
      // if (!this.vertify()) {
      //   return false
      // }
      if (this.$route.query.type == 1) {
        if (this.$route.query.id) {
          // 编辑
          agentUpdate(this.detail).then(data => {
            Is.tip('编辑成功', 2000, 'success')
            this.$router.back()
          })
        } else {
          // 新增
          agentAdd(this.detail).then(data => {
            Is.tip('添加成功', 2000, 'success')
            this.$router.back()
          })
        }
      }
      if (this.$route.query.type == 2) {
        if (this.$route.query.id) {
          // 编辑
          developersUpdate(this.detail).then(data => {
            Is.tip('编辑成功', 2000, 'success')
            this.$router.back()
          })
        } else {
          // 新增
          developersAdd(this.detail).then(data => {
            Is.tip('添加成功', 2000, 'success')
            this.$router.back()
          })
        }
      }
      if (this.$route.query.type == 3) {
        this.detail.enterpriseResources = []
        this.fileListGsfc.forEach(item => {
          this.detail.enterpriseResources.push({
            dataType: 0,
            pictureUrl: item,
            type: 0
          })
        })
        this.fileListYyzz.forEach(item => {
          this.detail.enterpriseResources.push({
            dataType: 0,
            pictureUrl: item,
            type: 1
          })
        })
        this.fileListIntroduce.forEach(item => {
          this.detail.enterpriseResources.push({
            dataType: 0,
            pictureUrl: item,
            type: 2
          })
        })
        if (this.$route.query.id) {
          // 编辑
          housePropertyUpdate(this.detail).then(data => {
            Is.tip('编辑成功', 2000, 'success')
            this.$router.back()
          })
        } else {
          // 新增
          housePropertytAdd(this.detail).then(data => {
            Is.tip('添加成功', 2000, 'success')
            this.$router.back()
          })
        }
      }
      if (this.$route.query.type == 4) {
        if (this.$route.query.id) {
          // 编辑
          memberUpdate(this.detail).then(data => {
            Is.tip('编辑成功', 2000, 'success')
            this.$router.back()
          })
        } else {
          // 新增
          memberAdd(this.detail).then(data => {
            Is.tip('添加成功', 2000, 'success')
            this.$router.back()
          })
        }
      }
    },
    // 返回
    cancel() {
      this.$router.back()
    },
    vertify() {
      if (Is.empty(this.detail.title)) {
        Is.tip('请输入标题')
        return false
      }
      if (Is.empty(this.detail.link)) {
        Is.tip('请输入链接')
        return false
      }
      if (Is.empty(this.detail.showType)) {
        Is.tip('请输入显示位置')
        return false
      }
      if (Is.empty(this.detail.sort)) {
        Is.tip('请输入排序')
        return false
      }
      if (Is.empty(this.fileList)) {
        Is.tip('请上传图片')
        return false
      }
      return true
    }
  }}
</script>
